﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace quickfileindexer.Components
{
    public class FileIndexer
    {
        public string[] FileNames;
        string indexpath = Environment.CurrentDirectory + "\\$RECYCLE.BIN\\_INDEX_";

        public bool Update()
        {
            try
            {
                File.Delete(indexpath);
            }
            catch (Exception) {}
            try
            {
                File.Create(indexpath);
            }
            catch (Exception) {}
            FileStream fs; StringBuilder sb = new StringBuilder();
            DirectoryInfo dir = new DirectoryInfo(Environment.CurrentDirectory);
            List<string> files = GetFilesByEx(dir);
            fs = new FileStream(indexpath, FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fs);
            StreamReader sr = new StreamReader(fs);
            foreach (string fileNames in files)
            {
                sb.Append(fileNames + "\n");
            }
            sw.Write(sb.ToString());
            this.FileNames = sr.ReadToEnd().Split('\n');
            sw.Close();
            return true;
        }

        private static List<string> GetFilesByEx(DirectoryInfo dir)
        {
            List<string> fileList = new List<string>();
            try
            {
                FileInfo[] allFile = dir.GetFiles("*");
                foreach (FileInfo fi in allFile)
                {
                    fileList.Add(fi.FullName);
                }
                DirectoryInfo[] allDir = dir.GetDirectories();
                foreach (DirectoryInfo d in allDir)
                {
                    fileList.AddRange(GetFilesByEx(d));
                }
            }
            catch (Exception) {}
            return fileList;
        }

        public void Search(string name, string extension)
        {
            FileStream fs = new FileStream(indexpath, FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            this.FileNames = sr.ReadToEnd().Split('\n');
            List<string> files = new List<string>();
            foreach (string file in this.FileNames)
            {
                int len = file.Split('.').Length;
                if (file.ToUpper().Contains(name.ToUpper()) && file.ToUpper().Split('.')[len-1] == extension.ToUpper()) files.Add(file);
            }
            new ResultForm(files,name).ShowDialog();
        }
        public void Search(string name)
        {
            FileStream fs = new FileStream(indexpath, FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            this.FileNames = sr.ReadToEnd().Split('\n');
            List<string> files = new List<string>();
            foreach (string file in this.FileNames)
            {
                if (file.ToUpper().Contains(name.ToUpper())) files.Add(file);
            }
            new ResultForm(files, name).ShowDialog();
        }
    }
}
